package account

import (
	"context"

	"gitee.com/spume/common_service/app/billing/api/account/v1"
	"gitee.com/spume/common_service/app/billing/api/schmea"
	"gitee.com/spume/common_service/app/billing/internal/model/do"
	"gitee.com/spume/common_service/app/billing/internal/service"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/util/gconv"
)

func (c *ControllerV1) AccountBalance(ctx context.Context, req *v1.AccountBalanceReq) (res *v1.AccountBalanceRes, err error) {
	res = &v1.AccountBalanceRes{Balances: map[int]schmea.Balance{}}
	balances, err := service.WalletAccount().Gets(ctx, &do.WalletAccount{
		MemberId:     req.MemberId,
		CurrencyType: req.CurrencyType,
		TenantId:     req.TenantId,
	})
	if err != nil {
		return
	}
	if g.IsEmpty(balances) {
		err = gerror.New("account not exist")
		return
	}
	for _, val := range balances {
		res.Balances[gconv.Int(val.CurrencyType)] = schmea.Balance{
			TenantId:     req.TenantId,
			MemberId:     req.MemberId,
			CurrencyType: gconv.Int(val.CurrencyType),
			Balance:      val.Balance,
			Block:        val.Block,
			ChangeTime:   val.UpdateTime,
		}
	}

	return
}
